package com.college.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.college.domain.Roles;
import com.college.interfaces.IRolesDAO;
import com.college.interfaces.IUserDAO;

@Controller
public class RolesController {
	@Autowired
	private IRolesDAO roleDAO;
	@Autowired
	private IUserDAO userDAO;

	@RequestMapping(value="/assignRole", method=RequestMethod.GET)
	public ModelAndView getRoleForm(ModelMap model)
	{
		List<Integer> userIdForDropDown = userDAO.getAllUserIdForDropDown();
		model.addAttribute("userIdDropDown",userIdForDropDown);
		ModelAndView mav = new ModelAndView("assign_role");
		Roles role = new Roles();
		mav.getModelMap().put("assign_role", role);
		return mav;
	}
	@RequestMapping(value="/assignRole", method=RequestMethod.POST)
	public String saveCollege(@ModelAttribute("assign_role")Roles role, BindingResult result, SessionStatus status, ModelMap model){
		if(result.hasErrors()){
			return "assign_role";
		}
		Integer res = roleDAO.saveRole(role);
		if(res!=null){
			status.setComplete();
			model.addAttribute("savedMsg", "User role saved Successfully.");
			
		}
		return "assign_role";
		
	}
	
}
